package com.zhangyujun.bysj.manager.config;

import com.zhangyujun.bysj.manager.interceptor.LoginAuthInterceptor;
import com.zhangyujun.bysj.manager.properties.UserProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Component
public class WebMvcConfiguration implements WebMvcConfigurer {
    @Autowired
    private LoginAuthInterceptor loginAuthInterceptor;
    @Autowired
    private UserProperties userProperties;

    //跨域配置
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")          //添加路径规则
                .allowCredentials(true)                //是否允许在跨域的情况下传递Cookie
                .allowedOriginPatterns("*")            // 允许请求来源的域规则
                .allowedMethods("*")
                .allowedHeaders("*");
    }

    //拦截器注册才可以使用
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginAuthInterceptor)
//                .excludePathPatterns("/admin/system/index/login",
//                        "/admin/system/index/generateValidateCode")  //哪些路径不需要拦截  登录  验证码
                .excludePathPatterns(userProperties.getNoAuthUrls()) //优化用读取配置文件的方法去取地址(用于解决地址过多不美观的问题)
                .addPathPatterns("/**");                             //哪些路径需要拦截
    }
}
